package cn.bdqn.service.impl;

import cn.bdqn.domain.Client;
import cn.bdqn.mapper.ClientMapper;
import cn.bdqn.service.ClientService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 王浩
 * @create 2020/9/19 9:04
 */
@Service("ClientService")
@Transactional
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ClientMapper mapper;

    /**
     * 添加客户信息
     * @param client
     */
    @Override
    public void AddClientInfo(Client client){
        client.setClientState(1);
        mapper.insert(client);
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @Override
    public Client queryInfoById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 根据手机号查询客户信息
     *
     * @param phone 电话
     * @return
     */
    @Override
    public Client queryInfoByPhone(String phone) {
        return mapper.selectByClientPhone(phone);
    }

    /**
     * 根据手机号判断手机号是否已注册
     *
     * @param phone 电话
     * @return
     */
    @Override
    public Boolean queryByPhoneIsExist(String phone) {
        if (mapper.selectByClientPhoneIsExist(phone) == 1){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 查询全部并分页
     *
     * @param pageNum   当前页码
     * @param pageAmount    每页条数
     * @return
     */
    @Override
    public Page queryAllInfo(Integer pageNum, Integer pageAmount) {
        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取全部客户信息
        List<Client> list = mapper.selectAllInfo();
        //分页
        PageInfo<Client> coursePageInfo = new PageInfo<>(list);
        //封装分页的信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());

        return page;
    }

    /**
     * 根据电话或姓名模糊查询信息并分页
     *
     * @param phone  客户电话
     * @param name   客户姓名
     * @param pageNum  当前页码
     * @param pageAmount  每页条数
     * @return
     */
    @Override
    public Page queryInfoByPhoneAndName(String phone, String name, Integer pageNum, Integer pageAmount) {

        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取客户信息
        List<Client> list = mapper.selectInfoLikePhoneOrLikeName(phone,name);
        //分页
        PageInfo<Client> coursePageInfo = new PageInfo<>(list);
        //封装分页的信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());

        return page;
    }

    /**
     * 逻辑删除
     * @param id 主键id
     */
    @Override
    public void alterStateById(Integer id) {
        mapper.updateState(id);
    }

    /**
     * 修改客户信息
     *
     * @param client
     */
    @Override
    public void alterInfo(Client client) {
        mapper.updateByPrimaryKeySelective(client);
    }

}
